package com.hu.uitl;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SignatureException;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class JwtUtils {
    private String secret = "MySecret";

    /**
     * 生成加密token
     */
    public String createToken(Map<String, Object> claims) {
        claims.put("date", System.currentTimeMillis() / 1000);
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, secret)
                .setClaims(claims)
                .compact();
    }

    /**
     * 解密token
     */
    public Map<String, Object> validateToken(String token) throws SignatureException {
        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
    }


}
